package com.eco.fanliapp.service.impl;

import com.alibaba.fastjson.JSON;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.TaoBaoPlanMapper;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.TaoBaoPlanService;
import com.eco.fanliapp.taobao.SearchController;
import com.eco.fanliapp.utils.JsonUtils;
import com.taobao.api.response.TbkScInvitecodeGetResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Service
public class TaoBaoPlanServiceImpl implements TaoBaoPlanService {

    @Autowired
    private TaoBaoPlanMapper taoBaoPlanMapper;
    @Autowired
    private SearchController searchController;

    @Override
    public ServerResponse<String> insert(TaobaoScheme taoBaoPlan) {
        taoBaoPlan.setCreateTime(new Date());
        taoBaoPlan.setUpdateTime(new Date());
        taoBaoPlan.setExpireTime(DateUtils.addDays(new Date(),29));
       //渠道邀请码
        try{

//            String s = ApiUtils.postEcoData(ApiEnum.TAOBAO_TBK_CINVITECODE, "session="+taobaoScheme.getAppSession());
            String s = "";
            TbkScInvitecodeGetResponse tbkScInvitecodeGetResponse = searchController.tbkScInvitecode(taoBaoPlan.getAppSession());
            if (tbkScInvitecodeGetResponse != null) {
                s = JSON.toJSONString(tbkScInvitecodeGetResponse);
            }

            TbkScInvitecodeGetResponse response = JsonUtils.getJsonToBean(s,TbkScInvitecodeGetResponse.class);
            if (response.getData() == null){

            }else {
                taoBaoPlan.setInviteCode(response.getData().getInviterCode());
            }

        } catch (Exception e) {

        }

        //获取会员邀请码
        try {
//            String sMember = ApiUtils.postEcoData(ApiEnum.TAOBAO_TBK_MINVITECODE, "session="+taobaoScheme.getAppSession());
            String sMember = "";
            TbkScInvitecodeGetResponse tbkScInvitecodeGetResponse = searchController.tbkMeInvitecode(taoBaoPlan.getAppSession());
            if (tbkScInvitecodeGetResponse != null) {
                sMember = JSON.toJSONString(tbkScInvitecodeGetResponse);
            }

            TbkScInvitecodeGetResponse responseMember = JsonUtils.getJsonToBean(sMember,TbkScInvitecodeGetResponse.class);
            if(responseMember.getData() == null){

            }else {
                taoBaoPlan.setMinviteCode(responseMember.getData().getInviterCode());
            }

        } catch (Exception e) {

        }
        int i = taoBaoPlanMapper.insert(taoBaoPlan);
        if(i == 1){
            return ServerResponse.createBySuccessMessage("添加推广位方案成功");
        }
        return ServerResponse.createByErrorMessage("添加推广位方案失败");
    }

//    @CachePut(key ="#taoBaoPlan.id+'taoBaoPlan'")
    @Override
    public ServerResponse<String> update(TaobaoScheme taoBaoPlan) {
        taoBaoPlan.setUpdateTime(new Date());
        HashMap<String,Object> hashMap = new HashMap<String,Object>();hashMap.put("id",taoBaoPlan.getId());
        taoBaoPlan.setExpireTime(DateUtils.addDays(new Date(),29));
        //渠道邀请码
        try{

//            String s = ApiUtils.postEcoData(ApiEnum.TAOBAO_TBK_CINVITECODE, "session="+taobaoScheme.getAppSession());
            String s = "";
            TbkScInvitecodeGetResponse tbkScInvitecodeGetResponse = searchController.tbkScInvitecode(taoBaoPlan.getAppSession());
            if (tbkScInvitecodeGetResponse != null) {
                s = JSON.toJSONString(tbkScInvitecodeGetResponse);
            }

            TbkScInvitecodeGetResponse response = JsonUtils.getJsonToBean(s,TbkScInvitecodeGetResponse.class);
            if (response.getData() == null){

            }else {
                taoBaoPlan.setInviteCode(response.getData().getInviterCode());
            }

        } catch (Exception e) {

        }

        //获取会员邀请码
        try {
//            String sMember = ApiUtils.postEcoData(ApiEnum.TAOBAO_TBK_MINVITECODE, "session="+taobaoScheme.getAppSession());
            String sMember = "";
            TbkScInvitecodeGetResponse tbkScInvitecodeGetResponse = searchController.tbkMeInvitecode(taoBaoPlan.getAppSession());
            if (tbkScInvitecodeGetResponse != null) {
                sMember = JSON.toJSONString(tbkScInvitecodeGetResponse);
            }

            TbkScInvitecodeGetResponse responseMember = JsonUtils.getJsonToBean(sMember,TbkScInvitecodeGetResponse.class);
            if(responseMember.getData() == null){

            }else {
                taoBaoPlan.setMinviteCode(responseMember.getData().getInviterCode());
            }

        } catch (Exception e) {

        }
        int i = taoBaoPlanMapper.update(taoBaoPlan);
        if(i == 1){
            return  ServerResponse.createBySuccessMessage("更新成功");
        }
        return  ServerResponse.createBySuccessMessage("更新失败");

    }

    @Override
    public ServerResponse<String> delete(int id) {
        int i =  taoBaoPlanMapper.delete(id);
        if(i>0){
            return  ServerResponse.createBySuccessMessage("删除成功");
        }else {
            return ServerResponse.createByErrorMessage("删除失败,该方案正在被使用不能删除");
        }

    }

    @Override
    public List<TaobaoScheme> findTaoBaoPlanAll(HashMap<String, Object> hashMap) {
        //判断hashMap是否为空
        Object companyId = null;
        if(null == hashMap){
            companyId = null;
        }else{
            companyId = hashMap.get("companyId");
        }
        List<TaobaoScheme> list = null;
        if(companyId == null){
            list = taoBaoPlanMapper.findTaoBaoPlanAll(hashMap);
        }else{
            list = taoBaoPlanMapper.findTaoBaoPlanByCompanyId(hashMap);
        }
        return list;
    }


    @Override
    public List<TaobaoScheme> findTaoBaoPlanStatus() {
        return taoBaoPlanMapper.findTaoBaoPlanStatus();
    }

    @Override
    public TaobaoScheme findTaoBaoPlanById(int id) {
        return taoBaoPlanMapper.findTaoBaoPlanById(id);
    }
}
